using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;



namespace ERPStudio
{
 
    public partial class FrmStabilizare : Form
    {
        [DllImport("kernel32.dll")]
        private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

        private UInt16 vCop = 0;
        private Autorizare oAuth = new Autorizare();
        public List<eInfoModule> _listaModule = null;
        public FrmStabilizare(List<eInfoModule> mod)
        {
            _listaModule = mod;
            InitializeComponent();
        }


       public string GetAllVolumeSerialNumber()
        {
            uint serNum = 0;
            uint maxCompLen = 0;
            StringBuilder VolLabel = new StringBuilder(256); // Label
            UInt32 VolFlags = new UInt32();
            StringBuilder FSName = new StringBuilder(256); // File System Name
            String strDriveLetter = "C:\\"; // fix up the passed-in drive letter for the API call
            long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

            return Convert.ToString(serNum);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder cale = new StringBuilder((System.Environment.GetCommandLineArgs()[0]).ToString());
            cale.Replace(".exe", ".din");
            cale.Replace(".vshost", null);
            if (oAuth.CreateDin(cale.ToString(),_listaModule))
                MessageBox.Show("Fisierul  " + cale + " a fost creat cu succes!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder cale = new StringBuilder((System.Environment.GetCommandLineArgs()[0]).ToString());
            cale.Replace(".exe", ".din");
            cale.Replace(".vshost", null);
            oAuth.CitesteDin(cale.ToString(),_listaModule);
            /*tBZileFunctionare.Text = st3;
            textBox1.Text = st2.Substring(0, 4);
            textBox2.Text = st2.Substring(4, 4);
            textBox3.Text = st2.Substring(8, 4);
            textBox4.Text = st2.Substring(12, 4);
            textBox5.Text = st2.Substring(16, 4);
            cBVarianta.SelectedIndex = var;*/

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // citeste valorile introduse in cheia de transmis
	        StringBuilder cheie = new StringBuilder(),cheie_rec = new StringBuilder();
		    cheie_rec.Append(textBox10.Text);
            cheie_rec.Append(textBox9.Text);
            cheie_rec.Append(textBox8.Text);
            cheie_rec.Append(textBox7.Text);
            cheie_rec.Append(textBox6.Text);
	
		    cheie.Append(textBox1.Text);
            cheie.Append(textBox2.Text);
            cheie.Append(textBox3.Text);
            cheie.Append(textBox4.Text);
            cheie.Append(textBox5.Text);


	        if(cheie_rec.Length==20)
            {
		        if(!oAuth.SuntEgaleCheile(cheie.ToString(),cheie_rec.ToString()))
                {
                    MessageBox.Show("Cheia introdusa este incorecta!", "Atentie", MessageBoxButtons.OK, MessageBoxIcon.Error);
			        return;
		        }
	        }
	        else
            {
		        if(cheie_rec.Length!=16)
                {
			        MessageBox.Show("Cheia introdusa este incorecta!","Atentie",MessageBoxButtons.OK,MessageBoxIcon.Error);
			        return;		
		        }
	        }
            if (oAuth.SaveAuth(this.ModuleCB.SelectedIndex, cheie_rec.ToString(),_listaModule))
            {
                MessageBox.Show("Autorizarea a decurs cu succes!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
		        return; 
	        }

            MessageBox.Show("Au aparut erori in procesul de autorizare!", "Atentie", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void textBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control)
            {
                // a apasat d
                if (vCop == 0 && e.KeyValue == 68)
                    vCop = 1;
                // a apasat s
                if (vCop == 1 && e.KeyValue == 83)
                    vCop = 2;
                if (vCop == 2 && e.KeyValue == 67)
                    vCop = 3;
                if (vCop == 3)
                {
                    if (MessageBox.Show("Doriti activarea anularii instalarii?", "Avertisment",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        btnAutorizeaza.Visible = true;
                        btnCreaza.Visible = true;
                        tBNrZile.Visible = true;
                        btnReLoad.Visible = true;
                        btnTemp.Visible = true;
                        this.mGID.Visible = true;
                        textBox1.ReadOnly = false;
                        textBox2.ReadOnly = false;
                        textBox3.ReadOnly = false;
                        textBox4.ReadOnly = false;
                        textBox5.ReadOnly = false;
                        idHdReceptionat.Visible = true;
                        idModulRec.Visible = true;
                        this.button1.Visible = true;
                        this.cBVarianta.Enabled = true;

                    }
                }

            }
            if (textBox6.Text.Length >= 3)
                this.btnAutorizeaza.Focus();
        }

        private void btnAutorizeaza_Click(object sender, EventArgs e)
        {
            StringBuilder  cheie_rec = new StringBuilder();
            cheie_rec.Append(textBox1.Text);
            cheie_rec.Append(textBox2.Text);
            cheie_rec.Append(textBox3.Text);
            cheie_rec.Append(textBox4.Text);
            cheie_rec.Append(textBox5.Text);
            String ret = CalcCheieStabilizare(cheie_rec.ToString());
            if (ret.Length >= 20)
            {
                textBox10.Text = ret.Substring(0, 4);
                textBox9.Text = ret.Substring(4, 4);
                textBox8.Text = ret.Substring(8, 4);
                textBox7.Text = ret.Substring(12, 4);
                textBox6.Text = ret.Substring(16, 4);
            }

        }
        private String CalcCheieStabilizare(String cheie)
        {
            Int16 idMod = 0;
            String hId = oAuth._GetHDIdCl(cheie,ref idMod);
            idModulRec.Text = idMod.ToString();
            idHdReceptionat.Text = hId;
            return oAuth.CalcCheieSerialTr(hId,idMod);
        }

        private void btnRenunta_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnTemp_Click(object sender, EventArgs e)
        {
            String ret = this.oAuth.CalcCheieTemp(tBNrZile.Text.ToString());
            if (ret.Length >= 16)
            {
                textBox10.Text = ret.Substring(0, 4);
                textBox9.Text = ret.Substring(4, 4);
                textBox8.Text = ret.Substring(8, 4);
                textBox7.Text = ret.Substring(12, 4);
                textBox6.Text = String.Empty;
            }
        }

        private void FrmStabilizare_Load(object sender, EventArgs e)
        {
            //citeste informatiile din fisierul de autorizare
            this.mGID.Text = GetAllVolumeSerialNumber();
            StringBuilder cale = new StringBuilder((System.Environment.GetCommandLineArgs()[0]).ToString());
            cale.Replace(".exe", ".din");
            cale.Replace(".vshost", null);
            try
            {
                oAuth.CitesteDin(cale.ToString(),_listaModule);
            }
            catch (Exception ex)
            {
                // nu trebuie inchisa macheta
                MessageBox.Show(ex.ToString());
            }
            

            cBVarianta.SelectedIndex = 0;
            foreach (eInfoModule ret in _listaModule)
            {
                if(ret.mModule!=null)
                    ret.IDLista = this.ModuleCB.Items.Add(ret.Name);
            }
            if (ModuleCB.Items.Count!=0)
                ModuleCB.SelectedIndex = 0;
            comboBox1_SelectedIndexChanged(ModuleCB, null);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String cheie = String.Empty;
            Int16 stare=0, zile=0;
            DateTime DataInst = DateTime.Now;
            oAuth.getModuleInfo((Int16)(ModuleCB.SelectedIndex), ref cheie, ref stare, ref zile, ref DataInst,_listaModule);
            if (cheie == null)
                return;
            tBZileFunctionare.Text = zile.ToString();
            if (cheie.Length >= 20)
            {
                textBox1.Text = cheie.Substring(0, 4);
                textBox2.Text = cheie.Substring(4, 4);
                textBox3.Text = cheie.Substring(8, 4);
                textBox4.Text = cheie.Substring(12, 4);
                textBox5.Text = cheie.Substring(16, 4);
            }
            this.cBVarianta.SelectedIndex = stare;
            this.DataInstalareDTP.Value = DataInst;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            textBox5.Text = String.Empty;
 
        }
    }
}